package net.suncaper.hotel.web.controller;

import net.suncaper.hotel.common.framework.RequestMapping;
import net.suncaper.hotel.common.utils.JSONUtil;
import net.suncaper.hotel.common.utils.PageInfo;
import net.suncaper.hotel.common.utils.Result;
import net.suncaper.hotel.model.dto.RoomDTO;
import net.suncaper.hotel.model.dto.RoomTypeDTO;
import net.suncaper.hotel.model.vo.RoomTypeVO;
import net.suncaper.hotel.model.vo.RoomVO;
import net.suncaper.hotel.service.IRoomService;
import net.suncaper.hotel.service.impl.RoomServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.List;

/**
 * 房间控制器
 */
@RequestMapping("/api/room")
public class RoomController {
    private IRoomService roomService = new RoomServiceImpl();

    /**
     * 获取房间类型列表
     */
    @RequestMapping("/type/list")
    public void getRoomTypeList(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<RoomTypeVO> list = roomService.getRoomTypeList();
            JSONUtil.printByJSON(response, Result.success(list));
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取房型列表失败"));
        }
    }

    /**
     * 创建房间类型
     */
    @RequestMapping("/type/create")
    public void createRoomType(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            
            RoomTypeDTO dto = JSONUtil.fromJSON(sb.toString(), RoomTypeDTO.class);
            boolean success = roomService.createRoomType(dto);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("创建房型成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("创建房型失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("创建房型失败"));
        }
    }

    /**
     * 更新房间类型
     */
    @RequestMapping("/type/update")
    public void updateRoomType(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            
            RoomTypeDTO dto = JSONUtil.fromJSON(sb.toString(), RoomTypeDTO.class);
            boolean success = roomService.updateRoomType(dto);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("更新房型成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("更新房型失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("更新房型失败"));
        }
    }

    /**
     * 删除房间类型
     */
    @RequestMapping("/type/delete/*")
    public void deleteRoomType(HttpServletRequest request, HttpServletResponse response) {
        try {
            String uri = request.getRequestURI();
            String idStr = uri.substring(uri.lastIndexOf("/") + 1);
            if (idStr.endsWith(".do")) {
                idStr = idStr.substring(0, idStr.length() - 3);
            }
            
            Integer id = Integer.parseInt(idStr);
            boolean success = roomService.deleteRoomType(id);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("删除房型成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("删除房型失败，可能还有房间使用该房型"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("删除房型失败"));
        }
    }

    /**
     * 获取房间列表
     */
    @RequestMapping("/list")
    public void getRoomList(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pageNumStr = request.getParameter("pageNum");
            String pageSizeStr = request.getParameter("pageSize");
            
            int pageNum = pageNumStr != null ? Integer.parseInt(pageNumStr) : 1;
            int pageSize = pageSizeStr != null ? Integer.parseInt(pageSizeStr) : 10;
            
            PageInfo<RoomVO> pageInfo = roomService.getRoomList(pageNum, pageSize);
            JSONUtil.printByJSON(response, Result.success(pageInfo));
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取房间列表失败"));
        }
    }

    /**
     * 根据状态获取房间
     */
    @RequestMapping("/listByStatus")
    public void getRoomsByStatus(HttpServletRequest request, HttpServletResponse response) {
        try {
            String status = request.getParameter("status");
            List<RoomVO> list = roomService.getRoomsByStatus(status);
            JSONUtil.printByJSON(response, Result.success(list));
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("获取房间列表失败"));
        }
    }

    /**
     * 创建房间
     */
    @RequestMapping("/create")
    public void createRoom(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            
            RoomDTO dto = JSONUtil.fromJSON(sb.toString(), RoomDTO.class);
            boolean success = roomService.createRoom(dto);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("创建房间成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("创建房间失败，房间号可能已存在"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("创建房间失败"));
        }
    }

    /**
     * 更新房间
     */
    @RequestMapping("/update")
    public void updateRoom(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            
            RoomDTO dto = JSONUtil.fromJSON(sb.toString(), RoomDTO.class);
            boolean success = roomService.updateRoom(dto);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("更新房间成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("更新房间失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("更新房间失败"));
        }
    }

    /**
     * 删除房间
     */
    @RequestMapping("/delete/*")
    public void deleteRoom(HttpServletRequest request, HttpServletResponse response) {
        try {
            String uri = request.getRequestURI();
            String idStr = uri.substring(uri.lastIndexOf("/") + 1);
            if (idStr.endsWith(".do")) {
                idStr = idStr.substring(0, idStr.length() - 3);
            }
            
            Integer id = Integer.parseInt(idStr);
            boolean success = roomService.deleteRoom(id);
            
            if (success) {
                JSONUtil.printByJSON(response, Result.success("删除房间成功"));
            } else {
                JSONUtil.printByJSON(response, Result.fail("删除房间失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONUtil.printByJSON(response, Result.error("删除房间失败"));
        }
    }
}